package com.sin.j2me.wihd.window;

import java.util.TimeZone;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.DateField;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;

import com.sin.j2me.framework.NewDebug;
import com.sin.j2me.framework.SinUtil;
import com.sin.j2me.util.AsynchTask;
import com.sin.j2me.wihd.FrameWork;
import com.sin.j2me.wihd.common.Commands;
import com.sin.j2me.wihd.common.Constant;
import com.sin.j2me.wihd.common.Protocol;
import com.sin.j2me.wihd.common.Strings;
import com.sin.j2me.wihd.communi.InterfaceRequest;
import com.sin.j2me.wihd.model.Param;
import com.sin.j2me.wihd.model.Response;
import com.sin.j2me.wihd.model.Thing;

public class DetailWindow extends Window implements CommandListener {
	
	public static final int ADD = 1;
	public static final int MOD = 2;
	
	private int status = ADD;
	
	private Form form = null;
	private TextField title = null;
	private TextField detail = null;
	javax.microedition.lcdui.Gauge gauge = null;
	
	private DateField time = null;
	
	private Thing thing = new Thing();
	public DetailWindow() {
		form = new Form(thing.getTitle());
		title = new TextField(Strings.title, null, Constant.LEN_TITLE, TextField.ANY);
		detail = new TextField(Strings.detail, null, Constant.LEN_DETAIL, TextField.ANY);
		time = new DateField(Strings.time, DateField.DATE_TIME, TimeZone.getDefault());

		form.append(title);
		form.append(time);
		form.append(detail);
		
		
		form.addCommand(Commands.ok);
		form.addCommand(Commands.back);
		
		form.setCommandListener(this);
	}
	
	public void startWith(Thing thing, int status){
		this.thing = thing;
		this.status = status;
		initForm();
	}
	
	public void initForm(){
		switch(this.status)
		{
			case ADD:
				form.setTitle(Strings.addthing);
				title.setString(this.thing.getTitle());
				detail.setString(this.thing.getDetail());
				time.setDate(SinUtil.getNow());
				break;
			case MOD:
				form.setTitle(this.thing.getTitle());
				title.setString(this.thing.getTitle());
				detail.setString(this.thing.getDetail());
				time.setDate(SinUtil.string2Date(this.thing.getTime()));
				break;
			default:
				NewDebug.e("unknown status: " + this.status);
				break;
		}
		FrameWork.startWindow(this);
	}
	
	public Displayable getWindow() {
		return form;
	}
	
	private boolean checkInput(){
		if(this.title.getString().trim().length() == 0){
			setNotice(Strings.N_EMPTY_TITLE);
			return false;
		}
		return true;
	}
	
	public void commandAction(Command cmd, Displayable dis) {
//		System.out.println("Cmd:" + cmd.getLabel());
		if(cmd == Commands.exit){
			FrameWork.exit();
		}
		else if(cmd == Commands.back){
			FrameWork.finishWindow(this);
		}
		else if(cmd == Commands.ok){
			switch(this.status)
			{
				case ADD:
					if(!checkInput()){
						return;
					}
					if(locked){
						setNotice(Strings.I_LOCKED);
						return;
					}
					this.thing.setTitle(title.getString());
					this.thing.setDetail(detail.getString());
					this.thing.setUserid(UO.getUserid());
					this.thing.setStatus(Strings.S_FINISHED);
					this.thing.setTime(SinUtil.date2String(time.getDate()));
					lock();
					new AsynchTask(new Runnable() {
						public void run() {
							Param param = new Param();
							param.addPam(Protocol.P_TITLE, thing.getTitle());
							param.addPam(Protocol.P_STATUS, thing.getStatus());
							param.addPam(Protocol.P_DETAIL, thing.getDetail());
							param.addPam(Protocol.P_TIME, thing.getTime());
							Response response = InterfaceRequest.getResponse(Protocol.I_ADDTHING, param);
							if(response.isOk()){
								String thingid = response.getString(Protocol.P_THINGID);
								thing.setThingid(Integer.parseInt(thingid));
								status = MOD;
								FrameWork.thingsWindow.addThing(thing, true);
								FrameWork.finishWindow(DetailWindow.this);
								setNotice(Strings.N_ADDOK);
							}
							else if(response.getStatus() == Response.FAIL){
								setNotice(response.getString(Protocol.R_INFO));
							}
							else{
								setNotice(Strings.P_NETWORKERR);
							}
							unlock();
						}
					});
					
					System.out.println(thing);
					break;
				case MOD:
					if(!checkInput()){
						return;
					}
					if(locked){
						setNotice(Strings.I_LOCKED);
						return;
					}
					this.thing.setTitle(title.getString());
					this.thing.setDetail(detail.getString());
					this.thing.setUserid(UO.getUserid());
					this.thing.setStatus(Strings.S_FINISHED);
					this.thing.setTime(SinUtil.date2String(time.getDate()));
					lock();
					new AsynchTask(new Runnable() {
						public void run() {
							Param param = new Param();
							param.addPam(Protocol.P_TITLE, thing.getTitle());
							param.addPam(Protocol.P_STATUS, thing.getStatus());
							param.addPam(Protocol.P_DETAIL, thing.getDetail());
							param.addPam(Protocol.P_TIME, thing.getTime());
							param.addPam(Protocol.P_THINGID, ""+thing.getThingid());
							Response response = InterfaceRequest.getResponse(Protocol.I_MODTHING, param);
							if(response.isOk()){
								FrameWork.thingsWindow.refreshItem(thing);
								setNotice(Strings.N_MODOK);
							}
							else if(response.getStatus() == Response.FAIL){
								setNotice(response.getString(Protocol.R_INFO));
							}
							else{
								setNotice(Strings.P_NETWORKERR);
							}
							unlock();
						}
					});
					break;

				default:
					NewDebug.e("unknown status: " + this.status);
					break;
					
			}
		}
	}
}
